package zhtt;

import org.junit.Test;
import zhtt.JunitTestUtil;
import zhtt.demo03_oneToMany_oneself.Column;
import zhtt.demo05_manyToMany.Course;
import zhtt.demo05_manyToMany.Student;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by zhtt on 2017/4/18.
 */
public class Demo02Test extends JunitTestUtil {

    /** 单向关联
     *保存一方：1、插入一方；2、插入多方；3、更新多方
     * **/
    @Test
    public void save(){
        zhtt.demo02_oneToMany_s.Country country=new zhtt.demo02_oneToMany_s.Country("contry01");
        zhtt.demo02_oneToMany_s.Minister minister=new zhtt.demo02_oneToMany_s.Minister("minister02");
        Set<zhtt.demo02_oneToMany_s.Minister> ministerSet=new HashSet<zhtt.demo02_oneToMany_s.Minister>();
        ministerSet.add(minister);
        country.setMinisterSet(ministerSet);
        session.save(country);
    }

    /** 双向关联
     * 保存多方：1、插入一方；2、插入多方
     * **/
    @Test
    public void save2(){
        zhtt.demo02_oneToMany_m.Country country=new zhtt.demo02_oneToMany_m.Country("contry01");
        zhtt.demo02_oneToMany_m.Minister minister=new zhtt.demo02_oneToMany_m.Minister("minister02");
        minister.setCountry(country);
        session.save(minister);
    }

    /**
     * 自关联
     */
    @Test
    public void save3(){
        Column column=new Column("A");
        Column childColumn=new Column("A-01");
        childColumn.setParentColumn(column);
        session.save(childColumn);
    }

    /**
     * 多对一关联
     */
    @Test
    public void save4(){
        zhtt.demo04_manyToOne_s.Country country=new zhtt.demo04_manyToOne_s.Country("contry04");
        zhtt.demo04_manyToOne_s.Minister minister=new zhtt.demo04_manyToOne_s.Minister("minister04");
        minister.setCountry(country);
        session.save(minister);
    }

    @Test
    public void save5(){
        zhtt.demo05_manyToMany.Student student=new zhtt.demo05_manyToMany.Student("001");
        zhtt.demo05_manyToMany.Course course=new zhtt.demo05_manyToMany.Course("00A");
        Set<zhtt.demo05_manyToMany.Course> courseSet=new HashSet<zhtt.demo05_manyToMany.Course>();
        courseSet.add(course);
        student.setCourseSet(courseSet);
        session.save(student);
    }

    @Test
    public void save6(){
        zhtt.demo06_manyToMany_m.Student student=new zhtt.demo06_manyToMany_m.Student("001");
        zhtt.demo06_manyToMany_m.Course course=new zhtt.demo06_manyToMany_m.Course("00A");
        Set<zhtt.demo06_manyToMany_m.Course> courseSet=new HashSet<zhtt.demo06_manyToMany_m.Course>();
        courseSet.add(course);
        student.setCourseSet(courseSet);
        session.save(student);
    }

}
